﻿//======================================================================
//
//        Copyright (C) 2015   
//        All rights reserved
//
//        filename : PutOutApply
//        description : 半成品/成品卡申领
//        created by 任卫华 at  20160108
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using System.Data;
using System.Text;

namespace CardLogistics.Web.MainPage.PutOutCard.PutOutApply
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.PutOutCard.PutOutApplyData DAL = new DAL.PutOutCard.PutOutApplyData();
        protected Dictionary<string, string> CardTypeList = null;
        public static string TitleName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsEdit)
            {
                CardTypeList = GetTypeList();
            }
            else
              CardTypeList = GetCardTypeList();
            if (!IsPostBack)
            {
                APPLYDATE.Value = DateTime.Now.ToString("yyyy-MM-dd");//申领日期
                InitControl();
            }
        }

        #region 获取卡类型
        /// <summary>
        /// 新增时调用此方法
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, string> GetCardTypeList()
        {
            Dictionary<string, string> cardTypeList = new Dictionary<string, string>();
            cardTypeList = DAL.GetCardTypeList();
            return cardTypeList;
        }
        /// <summary>
        /// 修改时调用此方法
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, string> GetTypeList()
        {
            // var BranchArray = CardTypeList.ToDictionary(k => k.ID, k => k.Name);可以把数据库查出来的list转换成Dictionary
            Dictionary<string, string> cardTypeList = new Dictionary<string, string>();
                 string demandId="";//需求单 外键Id
                  string _key = Request.QueryString["id"];
                  if (!string.IsNullOrEmpty(_key))
                  {
                      CardLogistics.Model.PutOutCard.PutOutApply pData = DAL.GetModel(_key);
                      demandId = pData.DEMANDID; //需求单外键Id
                  }
                  cardTypeList = DAL.GetTypeList(demandId);
                  return cardTypeList;
        }
        #endregion

        #region 编辑时绑定值
        /// <summary>
        /// 编辑时绑定值
        /// </summary>
        public void InitControl()
        {
            TitleName = GetStatusInfo(0, "半成品/成品卡申领");
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                   CardLogistics.Model.PutOutCard.PutOutApply pData = DAL.GetModel(_key);
                    SetWebControls(this.Page, pData);
                    hf_DemandId.Value = pData.DEMANDID; //需求单外键Id
                    CardTypeId.Value =pData.CARDTYPE; //卡片类型Id
                    ApplyTotal.Value =pData.CARDNUMBER.ToString(); //申领数量
                    hf_EditSumCard.Value = ApplyTotal.Value; //存储编辑前的卡片数量
                    btn_Add.Visible = btn_Submit.Visible = false;
                    if (pData.ISSUBMIT == 0 || pData.ISSUBMIT == 3)//状态为 0(未提交) 或 3(驳回)时才可以进行编辑操作
                    {
                        btn_Add.Visible = btn_Submit.Visible = true;
                    }
                    TitleName = GetStatusInfo(int.Parse(pData.ISSUBMIT.ToString()), "半成品/成品卡申领");

                    //通过需求单ID得到需求总数量和已被申领的总数量
                    DataTable demanddt = CardLogistics.DAL.CommonData.GetList("*", "v_putoutdemand_detail", "ID='" + pData.DEMANDID + "'");
                    if (demanddt.Rows.Count>0)
                    {
                        DemTotal.Value = demanddt.Rows[0]["DemTotal"].ToString(); //需求总数量
                        AppedTotal.Value = demanddt.Rows[0]["AppedTotal"].ToString();//已被申领数量
                    }
                }
            }
            else
            {
                hf_EditSumCard.Value = "0";
                APPLYNO.Value = GetBillNumber("半成品/成品卡申领");
            }

        }
        #endregion

        #region 保存事件
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = EncapsulationModel(0);
                ShowMegByResult(result,0);
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        #endregion

        #region 保存并提交事件
        /// <summary>
        /// 保存并提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                string result = EncapsulationModel(1);
                ShowMegByResult(result, 1);
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        #endregion

        #region 提示信息
        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="result"></param>
        private void ShowMegByResult(string result, int issubmit)
        {
            if (result.Split(',')[0] == "ok")
            {
                if (issubmit == 1)
                {
                    InsertAppInfo(7, result.Split(',')[1]); // 7半成品/成品卡申领
                    InsertLog("保存并提交半成品/成品卡申领信息,申领单号:" + APPLYNO.Value);
                }
                else
                    InsertLog("保存半成品/成品卡申领信息,申领单号:" + APPLYNO.Value);
                ReturnListInfo();
            }
            else
            {
                ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
            }
        }
        #endregion 

        #region 申领信息
        /// <summary>
        /// 申领信息
        /// </summary>
        /// <param name="issubmit">是否提交</param>
        /// <returns></returns>
        private string EncapsulationModel(int issubmit)
        {
            string result = string.Empty;
            CardLogistics.Model.PutOutCard.PutOutApply model = new CardLogistics.Model.PutOutCard.PutOutApply();
            model = GetWebControls<CardLogistics.Model.PutOutCard.PutOutApply>(this.Page);
            model.ISSUBMIT = issubmit;
            model.LASTUPDATEUSER = LoggedUser.ID;
            model.LASTUPDATEDATE = DateTime.Now;
            model.ADDTIME = DateTime.Now;
            model.DEMANDID = hf_DemandId.Value; //需求单 外键Id
            model.ISDELETE = 0; //是否删除 0 未删除 1 已删除
            model.CARDTYPE = CardTypeId.Value; //卡片类型名称
            model.CARDNUMBER = Convert.ToInt32(ApplyTotal.Value);//申领数量
            if (IsEdit)
            {
                model.ID = Request.QueryString["id"];
                result = DAL.Update(model);
            }
            else
            {
                result = DAL.Add(model);
                if (result.Split(',')[0] == "ok")
                {
                    ChgBillNumber("半成品/成品卡申领");
                }
            }
            return result;
        }
        #endregion

        #region 通过选择需求单得到申领数量
        /// <summary>
        /// 通过需求单Id得到申领数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DemandNum_Click(object sender, EventArgs e)
        {
            string conId = hf_DemandId.Value;
            DataTable dt = DAL.GetInfoByDemandId(conId);
            DataRow[] newdr = dt.Select(" IsSubmit<>4 ");//过滤有效的发放数据
            if (newdr.Length > 0)
            {
                dt = newdr.CopyToDataTable();
                if (dt.Rows.Count > 0)
                    //申领数量=需求总数量-已申领数量
                    ApplyTotal.Value = (Convert.ToInt32(DemTotal.Value) - Convert.ToInt32(dt.Compute("sum(CardNumber)", ""))).ToString(); //申领数量
            }
            else
                //申领数量=需求数量
                ApplyTotal.Value = Convert.ToInt32(DemTotal.Value).ToString();
            }
        #endregion
    }
}